//
//  ShopViewController.m
//  随便点
//
//  Created by ZC on 15/4/6.
//  Copyright (c) 2015年 qfsoft. All rights reserved.
//

#import "ShopViewController.h"
#import "ShopView.h"
#import "AppDelegate.h"
#import "BMapKit.h"
#import "TourLocation.h"

@interface ShopViewController (){
        ShopView *shopView;
        AppDelegate *dele;
        NSString *countEvaluate;
        NSDictionary *shopDict; //店铺信息字典
        NSString *str1;     //店铺详情网址
        NSString *str2;     //店铺id
        NSString *str3;     //店铺完整接口
    BMKMapView *mapview;
    
}

@end

@implementation ShopViewController

- (void)viewDidLoad {
    self.navigationItem.hidesBackButton = YES;
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed: @"back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = item;
    
    dele = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    
    //加载自定义的scrollview
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ShopView" owner:self options:nil];
    shopView = nib[0];
    //框架scrollview的大小
    self.ShopScrollView.contentSize = CGSizeMake(self.view.frame.size.width, shopView.frame.size.height);
    //把自定义的scrollview添加到框架scrollview上
    [self.ShopScrollView addSubview:shopView];
    
    shopDict=[NSDictionary dictionary];
    
    //加载店铺信息
    [self ShopMessage];
    
    //加载评价数量
    [self EvaluateCount];
    
    [self dt];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)dt{
    mapview = [[BMKMapView alloc]initWithFrame:CGRectMake(8, 509, 359, 219)];
    [shopView addSubview:mapview];
    mapview.zoomLevel = 15;
    mapview.delegate = self;
}
-(void)setShowMapLongitude:(double)longitude latitude:(double)latitude{
    //设置经纬度
    CLLocationCoordinate2D coord={latitude,longitude};
    //设置显示范围精度
    BMKCoordinateSpan span={0.05,0.05};
    
    //显示范围
    BMKCoordinateRegion region={coord,span};
    
    //给地图设置显示区域
    [mapview setRegion:region animated:YES];
    
    TourLocation *tourLocation=[[TourLocation alloc]init];
    tourLocation.coordinate=CLLocationCoordinate2DMake(latitude,longitude);
    
    self.coordinate = CLLocationCoordinate2DMake(latitude,longitude);
    
    mapview.zoomLevel = 17;
    
    [mapview showsUserLocation];
    
    [mapview addAnnotation:tourLocation];
}
-(void)ShopMessage{
    
    str1 = @"http://www.51anys.com:8080/waimaiwang/shop/details";
    str2 = [NSString stringWithFormat:@"%@",dele.spid];  //店铺ID
    str3 =[NSString stringWithFormat:@"%@/%@",str1,str2];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:str3]];
    NSString *postStr=@"";
    [request setHTTPMethod:@"post"];
    NSData *data=[postStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError){
        shopDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        [self performSelectorOnMainThread:@selector(refreshData) withObject:shopDict waitUntilDone:YES];
        
        
        //图片地址
        NSString *haha=@"http://www.51anys.com:8080/waimaiwang/resources/";
        NSString *hehe=[NSString stringWithFormat:@"%@",shopDict[@"image"]];
        NSString *string=[NSString stringWithFormat:@"%@%@",haha,hehe];
        NSURL *url=[NSURL URLWithString:string];
        
        shopView.shopImage.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
        shopView.shopName.text=[NSString stringWithFormat:@"%@",shopDict[@"shopName"]];
        shopView.shopAddress.text=[NSString stringWithFormat:@"%@",shopDict[@"address"]];
        shopView.shopPhone.text=[NSString stringWithFormat:@"%@",shopDict[@"dcdh"]];
        shopView.shopTime.text=[NSString stringWithFormat:@"%@",shopDict[@"time"]];
        shopView.shopDistribution.text=[NSString stringWithFormat:@"%@",shopDict[@"psje"]];
        shopView.shopIntroduce.text=[NSString stringWithFormat:@"%@",shopDict[@"describe"]];
        self.jingdu = shopDict[@"jingdu"];
        self.weidu = shopDict[@"weidu"];
        [self setShowMapLongitude:[self.jingdu floatValue] latitude:[self.weidu floatValue]];
    }];
}


-(void)refreshData{
    
}

-(void)EvaluateCount{
    str1 = @"http://www.51anys.com:8080/waimaiwang/shop/countEvaluate/";
    str2 = [NSString stringWithFormat:@"%@",dele.spid];  //店铺ID
    str3 =[NSString stringWithFormat:@"%@%@",str1,str2];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:str3]];
    NSString *postStr=@"";
    [request setHTTPMethod:@"post"];
    NSData *data=[postStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError){
        countEvaluate = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        [self performSelectorOnMainThread:@selector(refreshData) withObject:shopDict waitUntilDone:YES];
        shopView.shopEvaluation.text=[NSString stringWithFormat:@"%@",countEvaluate];
        
        dele.evaluateCount = shopView.shopEvaluation.text;
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
